package org.sbbs.app.license.webapp.action;

import java.util.List;

import org.sbbs.app.license.model.VenderInfo;
import org.sbbs.app.license.service.ProductInfoManager;
import org.sbbs.app.license.service.VenderInfoManager;
import org.sbbs.base.search.PropertySearchBuilder;
import org.sbbs.base.webapp.action.BaseGridAction;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;

public class ProductInfoGridAction
    extends BaseGridAction {
    /**
     *
     */
    private static final long serialVersionUID = -3938015919352134327L;

    private ProductInfoManager productInfoManager;

    private VenderInfoManager venderInfoManager;

    @Override
    public String gridPageList() {
        VenderInfo vender = null;
        List<VenderInfo> vl = this.venderInfoManager.findAll();
        int ls = vl.size();
        if ( ls == 0 ) {
            this.setAjaxMessage( getText( "productInfo.loadError1" ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
            // return ERROR;
        }
        else if ( ls > 1 ) {
            this.setAjaxMessage( getText( "productInfo.loadError2" ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
            // return ERROR;
        }
        else {
            vender = vl.get( 0 );

            Search search = PropertySearchBuilder.BuildSearch( getRequest() );
            search.addFilterEqual( "venderInfo.venderId", vender.getVenderId() );
            SearchResult srt = this.productInfoManager.searchAndCount( search );

            this.setPageList( srt.getResult() );
            this.records = srt.getTotalCount();
            this.total = getTotlePage( records, search.getMaxResults() );
            this.page = search.getPage() + 1;
        }
        return SUCCESS;
    }

    /*
     * private VenderInfo getVenderInfo() { List<VenderInfo> vl = this.venderInfoManager.findAll(); int ls = vl.size();
     * if ( ls == 0 ) return null; else return vl.get( 0 ); }
     */

    public ProductInfoManager getProductInfoManager() {
        return productInfoManager;
    }

    public void setProductInfoManager( ProductInfoManager productInfoManager ) {
        this.productInfoManager = productInfoManager;
    }

    public VenderInfoManager getVenderInfoManager() {
        return venderInfoManager;
    }

    public void setVenderInfoManager( VenderInfoManager venderInfoManager ) {
        this.venderInfoManager = venderInfoManager;
    }
}
